package com.cheeven.amazonbooksreviewscores;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class GetNewReleases {

    public static void main(String[] args) throws Exception {

        String fileName = "new-releases.txt";
        if (args.length > 0) {
            fileName = args[0];
        }

        GetPage page = new GetPage();
        Document doc = page.getPageDOM(new URL("http://www.amazon.co.uk"));
        String href = page.getPageInfo(doc, "//div[@id='navSaGwBody']/ul[1]/li[1]//ul/li/a[text()='Books']/@href");
        System.out.println("Homepage: " + href);

        doc = page.getPageDOM(new URL("http://www.amazon.co.uk" + href));
        href = page.getPageInfo(doc, "//tr[@id='navSubnavRowTR']/td/table//a[contains(text(), 'New') and contains(text(), 'Releases')]/@href");
        System.out.println("New Release: " + href);

        doc = page.getPageDOM(new URL("http://www.amazon.co.uk" + href));
        href = page.getPageInfo(doc, "//div[@id='leftNavContainer']//ul//li//a/span[text()='Last 30 days']/../@href");
        //href = page.getPageInfo(doc, "//div//ul//li"); //a[contains(text(), 'Last')]");// and contains(text(), '30') and contains(text(), 'days')]");
        System.out.println("Last 30 days: " + href);
        
        href = "http://www.amazon.co.uk/gp/search/ref=sr_adv_b/?search-alias=stripbooks&unfiltered=1&__mk_en_GB=%C5M%C5Z%D5%D1&field-keywords=&field-author=&field-title=&field-isbn=&field-publisher=&node=&field-binding_browse-bin=&field-subject=&emi=&field-dateop=After&field-datemod=8&field-dateyear=2011&sort=-pubdate&Adv-Srch-Books-Submit.x=59&Adv-Srch-Books-Submit.y=11";
        System.out.println("Last 30 days: " + href);
        
        PrintWriter output = new PrintWriter(new FileWriter(fileName));

        //ArrayList<HashMap<String, String>> books = new ArrayList<HashMap<String, String>>(4800);
        while (href != null) {
            doc = page.getPageDOM(new URL(href));
            NodeList results = page.getPageInfoNodeList(doc, "//div[@id='rightResultsATF']//div[contains(@id, 'result_')]");
            for (int i = 0; i < results.getLength(); i++) {
                Element result = (Element) results.item(i);
                HashMap<String, String> bookInfo = new HashMap<String, String>();
                bookInfo.put("asin", page.getPageInfo(result, "@name"));
                bookInfo.put("rank", page.getPageInfo(result, "div[@class='number']"));
                bookInfo.put("title", page.getPageInfo(result, "div[@class='productData']/div[@class='productTitle']/a").replace("\t", " "));
                bookInfo.put("urlString", page.getPageInfo(result, "div[@class='productData']/div[@class='productTitle']/a/@href"));
                bookInfo.put("author", page.getPageInfo(result, "div[@class='productData']/div[@class='productTitle']/span[@class='ptBrand']"));
                bookInfo.put("binding", page.getPageInfo(result, "div[@class='productData']/div[@class='productTitle']/span[@class='binding']"));

                System.out.println(bookInfo);

                output.print(bookInfo.get("rank") + "\t");
                output.print(bookInfo.get("asin") + "\t");
                output.print(bookInfo.get("title") + "\t");
                output.print(bookInfo.get("urlString") + "\t");
                output.print(bookInfo.get("image") + "\t");
                output.println();

                //books.add(bookInfo);
            }
            
            output.flush();

            String nextHref = page.getPageInfo(doc, "//a[@id='pagnNextLink']/@href");
            System.out.println(nextHref);

            if (href.equalsIgnoreCase(nextHref)) {
                break;
            }

            href = nextHref;
        }

        output.close();
    }
}
